package com.peng.leetcode.other;

/**
 * MaxSubArray
 *
 * @author: lupeng6
 * @create: 2020/11/28 17:15
 */
public class MaxSubArray {

    public static void main(String[] args) {
        int[] arr = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
        System.out.println(new Solution().maxSubArray(arr));
    }


    static class Solution {
        public int maxSubArray(int[] nums) {
            int curSum = 0;
            int maxSum = nums[0];
            for (int i : nums) {
                curSum = Math.max(curSum + i, i);
                maxSum = Math.max(curSum, maxSum);
            }
            return maxSum;
        }
    }
}
